import requests
import json
import time
import execjs
def first():
    # 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))
    headers = {
        'authority': 'www.geetest.com',
        'accept': 'application/json, text/javascript, */*; q=0.01',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'cache-control': 'no-cache',
        'pragma': 'no-cache',
        'referer': 'https://www.geetest.com/demo/fullpage.html',
        'sec-ch-ua': '"Not/A)Brand";v="99", "Microsoft Edge";v="115", "Chromium";v="115"',
        'sec-ch-ua-mobile': '?1',
        'sec-ch-ua-platform': '"Android"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36 Edg/115.0.1901.188',
        'x-requested-with': 'XMLHttpRequest',
    }

    params = {
        't': timestamp,
    }

    response = requests.get('https://www.geetest.com/demo/gt/register-fullpage', params=params,
                            headers=headers)
    if response.status_code == 200:
        data = json.loads(response.text)
        gt=data["gt"]
        challenge = data["challenge"]
        return gt,challenge
    else:
        print("请求失败，状态码:", response.status_code)
def third(gt,challenge,w):
    timestamp = str(int(time.time() * 1000))



    headers = {
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Pragma': 'no-cache',
        'Referer': 'https://www.geetest.com/',
        'Sec-Fetch-Dest': 'script',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36 Edg/115.0.1901.188',
        'sec-ch-ua': '"Not/A)Brand";v="99", "Microsoft Edge";v="115", "Chromium";v="115"',
        'sec-ch-ua-mobile': '?1',
        'sec-ch-ua-platform': '"Android"',
    }

    response = requests.get(
        'https://apiv6.geetest.com/get.php?gt='+gt+'&challenge='+challenge+'&lang=zh-cn&pt=3&client_type=web_mobile&w='+w+'&callback=geetest_'+timestamp,
        headers=headers,
    )
    response_text = response.text
    json_data = response_text.split('(', 1)[1].rsplit(')', 1)[0]
    data = json.loads(json_data)
    return data['data']['s']
def fourth(gt,challenge,w):
    timestamp = str(int(time.time() * 1000))
    headers = {
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Pragma': 'no-cache',
        'Referer': 'https://www.geetest.com/',
        'Sec-Fetch-Dest': 'script',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36 Edg/115.0.1901.188',
        'sec-ch-ua': '"Not/A)Brand";v="99", "Microsoft Edge";v="115", "Chromium";v="115"',
        'sec-ch-ua-mobile': '?1',
        'sec-ch-ua-platform': '"Android"',
    }

    response = requests.get(
        'https://api.geevisit.com/ajax.php?gt='+gt+'&challenge='+challenge+'&lang=zh-cn&pt=3&client_type=web_mobile&w='+w+'&callback=geetest_'+timestamp,
        headers=headers,
    )
    response_text = response.text
    json_data = response_text.split('(', 1)[1].rsplit(')', 1)[0]
    data = json.loads(json_data)
    print(data)
for _ in range(5):
    gt, challenge = first()
    start_time = time.time()
    with open('极验三代无感.js', 'r', encoding="utf-8") as f:
        js_code = f.read()
    # 执行JS函数
    context = execjs.compile(js_code)
    result = context.call('get_w1_value', gt, challenge)
    s = third(gt, challenge, result)
    # 执行JS函数
    result = context.call('get_w2_value', gt, challenge, s)
    fourth(gt, challenge, result)
    end_time = time.time()
    execution_time = int((end_time - start_time) * 1000)
    print("识别所用时间：", execution_time, "毫秒")
